var container;
var camera, scene, renderer, controls, effect;
var dom = $("#container");
var width = window.innerWidth;
var height = window.innerHeight;
dom.height(height);
init(dom);
animate();
function init( dom ) {
	container = dom[0];
	camera = new THREE.PerspectiveCamera( 40, width/height, 100, 20000 );
	camera.position.set( 0.0, 100, 100 * 3.5 );
	scene = new THREE.Scene();
	scene.background = new THREE.Color( "#4B0082" );
	scene.add( new THREE.AmbientLight( "#eeeeee") );//环境光源
//===============================文字2====================================
//	var loader = new THREE.FontLoader(); 
////	 loader.load("fonts/gentilis_regular.typeface.json", function (res) {
//	 loader.load("fonts/FZLanTingHeiS-UL-GB_Regular.json", function (res) {
//		 window.res = res;
//		 var font = new THREE.TextBufferGeometry("equipment走", {
//			  font: res,
//			  size: 5,
//			  height: 1
//		  }); 
//		  font.computeBoundingBox(); // 运行以后设置font的boundingBox属性对象，如果不运行无法获得。 
//		  font.computeVertexNormals(); 
//		  var material = new THREE.MeshLambertMaterial({side:THREE.DoubleSide,color: "#00ff00"}); 
//		  fontModel = new THREE.Mesh(font,material); 
//		  fontModel.position.set(0,0,0 );
//		  scene.add(fontModel); 
//	 }); 
//===============================走线====================================
//	var geometry_line = new THREE.Geometry();   //声明一个几何体
//    var material_line = new THREE.LineBasicMaterial({ color: "#ffff00" ,linewidth: 10});   //定义材质外观
//    var p1_line = new THREE.Vector3( 0, 0, 0 );
//    var p2_line = new THREE.Vector3( 500, 0, 500 );
//    geometry_line.vertices.push(p1_line); //vertices属性里可以存放点对象
//    geometry_line.vertices.push(p2_line);
//    var line = new THREE.Line(geometry_line, material_line, THREE.LinePieces);    //定义一条线
//    scene.add(line);   //加入场景里
  //===================================================================
   

//	grid = new THREE.GridHelper( 1000, 100, "#E0FFFF", "#9370DB" );
//	//圆形坐标系
////				grid = new THREE.PolarGridHelper( 1, 1, 10, 100, "#0000ff", "#ffa000" );
//	grid.rotateOnAxis( new THREE.Vector3( 0, 0, 0 ), 90 * ( Math.PI/180 ) );
//	scene.add( grid );//设置网格
	
	//添加数据
	var data = [
	            {value:[0,0,-40],color:"#8A2BE2",x:"",y:"ework",desc:""}
	            ,{value:[10,203.49,-40],color:"#8A2BE2",x:"",y:"",desc:"20349"}
	            ,{value:[20,177.77,-40],color:"#8A2BE2",x:"",y:"",desc:"17777"}
	            ,{value:[30,155.88,-40],color:"#8A2BE2",x:"",desc:"15588"}
	            ,{value:[40,178.61,-40],color:"#8A2BE2",x:"",y:""}
	            ,{value:[50,126.22,-40],color:"#8A2BE2",x:"",y:""}
	            ,{value:[0,66.2,-10],color:"#9370DB",x:"03月",y:"skycloud",desc:"6620"}
	            ,{value:[10,5.16,-10],color:"#9370DB",x:"04月",y:"",desc:"516"}
	            ,{value:[20,4.47,-10],color:"#9370DB",x:"05月",y:"",desc:"447"}
	            ,{value:[30,6.84,-10],color:"#9370DB",x:"06月",y:"",desc:"649"}
	            ,{value:[40,18.19,-10],color:"#9370DB",x:"07月",y:"",desc:""}
	            ,{value:[50,18.43,-10],color:"#9370DB",x:"08月",desc:"1843"}
	            ,{value:[0,0,-20],color:"#6A5ACD",x:"",y:"iglobalwin",desc:""}
	            ,{value:[10,3.15,-20],color:"#6A5ACD",x:"",y:"",desc:""}
	            ,{value:[20,7.54,-20],color:"#6A5ACD",x:"",y:"",desc:""}
	            ,{value:[30,7.77,-20],color:"#6A5ACD",x:"",y:"",desc:""}
	            ,{value:[40,10.38,-20],color:"#6A5ACD",x:"",y:"",desc:"1038"}
	            ,{value:[50,8.12,-20],color:"#6A5ACD",x:""}
	            ,{value:[0,3.11,-30],color:"#6495ED",x:"",y:"OA",desc:""}
	            ,{value:[10,3.84,-30],color:"#6495ED",x:"",y:"",desc:""}
	            ,{value:[20,3.16,-30],color:"#6495ED",x:"",y:"",desc:""}
	            ,{value:[30,2.34,-30],color:"#6495ED",x:"",y:"",desc:""}
	            ,{value:[40,3.36,-30],color:"#6495ED",x:"",y:"",desc:""}
	            ,{value:[50,2.38,-30],color:"#6495ED",x:""}
	            ];
	var position = [50,0,40];
	loadFont(function(a){
		addChart(scene,data,position);
		addLine(scene,data,position);
	});
	
	
	renderer = new THREE.WebGLRenderer( { antialias: true } );
	renderer.setPixelRatio( window.devicePixelRatio );
	renderer.setSize( width,height);
	container.appendChild( renderer.domElement );
	renderer.gammaInput = true;
	renderer.gammaOutput = true;
	controls = new THREE.OrbitControls( camera );
	window.addEventListener( 'resize', onWindowResize, false );
}
function onWindowResize() {
	camera.aspect = width/height;
	camera.updateProjectionMatrix();
	renderer.setSize( width,height );
}
function animate() {
	requestAnimationFrame( animate );
	renderer.render( scene, camera );
}
function addChart(scene,data,position){
	var map_mater = {};

	for(var i=0;i<data.length;i++){
		var d = data[i];
		var x = d.value[0];
		var y = d.value[1];
		var z = d.value[2];
		var color = d.color;
		if(typeof map_mater[color] == 'undefined'){
			map_mater[color] = new THREE.MeshLambertMaterial( {
				color: color,
				side: 0
			});
		}
		//柱状图
		obj = new THREE.Mesh( new THREE.CylinderBufferGeometry( 2, 2, y, 4, 0 ), map_mater[color] );
		obj.position.set( x+position[0], y/2+position[1], z+position[2] );
//		obj.rotation.y = Math.PI / 4;
		scene.add(obj);
		//球
		var sphereGeometry = new THREE.SphereGeometry( 2, 20, 20 );
	//	var sphereMaterial = new THREE.MeshPhongMaterial( { color: '#ff00ff', emissive: "#ff0000" } );
		var sphereMaterial = new THREE.MeshBasicMaterial({color: "#111111",transparent:true,blending:THREE.AdditiveBlending})
		sphere = new THREE.Mesh( sphereGeometry, sphereMaterial );
		sphere.position.set(x+position[0], y+position[1], z+position[2]);
		scene.add( sphere );
		
		//x系列字
		if(typeof d.x != 'undefined' && d.x!=""){
			var font = new THREE.TextBufferGeometry(d.x, {
				font: res,
				size: 2,
				height: 1
			}); 
			var fontModel = new THREE.Mesh(font,map_mater[color]); 
			fontModel.position.set(x+position[0], position[1], z+position[2]+10);
			scene.add(fontModel); 
		}
		//y系列字
		if(typeof d.y != 'undefined' && d.y!=""){
			var font = new THREE.TextBufferGeometry(d.y, {
				font: res,
				size: 2,
				height: 1
			}); 
			var fontModel = new THREE.Mesh(font,map_mater[color]); 
			fontModel.position.set(x+position[0]-20, position[1], z+position[2]);
			scene.add(fontModel); 
		}
		//desc
		if(typeof d.desc != 'undefined' && d.desc!=""){
			var font = new THREE.TextBufferGeometry(d.desc, {
				font: res,
				size: 2,
				height: 1
			}); 
			var fontModel = new THREE.Mesh(font,map_mater[color]); 
			fontModel.position.set(x+position[0]-2, y+position[1], z+position[2]);
			scene.add(fontModel); 
		}
	}
	  //添加图
	  material = new THREE.MeshLambertMaterial( {
			color: "#239800",
			side: 0
		} );
		object = new THREE.Mesh( new THREE.CylinderBufferGeometry( 4, 4, 40, 4, 0 ), material );
		object.position.set( -50, 20, 40 );
//		object.rotation.y = Math.PI / 4;
		scene.add( object );
}

//添加数据
var data = [
            {value:[0,0,-40],color:"#8A2BE2",x:"",y:"ework",desc:""}
            ,{value:[10,203.49,-40],color:"#8A2BE2",x:"",y:"",desc:"20349"}
            ,{value:[20,177.77,-40],color:"#8A2BE2",x:"",y:"",desc:"17777"}
            ,{value:[30,155.88,-40],color:"#8A2BE2",x:"",desc:"15588"}
            ,{value:[40,178.61,-40],color:"#8A2BE2",x:"",y:""}
            ,{value:[50,126.22,-40],color:"#8A2BE2",x:"",y:""}
            ,{value:[0,66.2,-10],color:"#9370DB",x:"03月",y:"skycloud",desc:"6620"}
            ,{value:[10,5.16,-10],color:"#9370DB",x:"04月",y:"",desc:"516"}
            ,{value:[20,4.47,-10],color:"#9370DB",x:"05月",y:"",desc:"447"}
            ,{value:[30,6.84,-10],color:"#9370DB",x:"06月",y:"",desc:"649"}
            ,{value:[40,18.19,-10],color:"#9370DB",x:"07月",y:"",desc:""}
            ,{value:[50,18.43,-10],color:"#9370DB",x:"08月",desc:"1843"}
            ,{value:[0,0,-20],color:"#6A5ACD",x:"",y:"iglobalwin",desc:""}
            ,{value:[10,3.15,-20],color:"#6A5ACD",x:"",y:"",desc:""}
            ,{value:[20,7.54,-20],color:"#6A5ACD",x:"",y:"",desc:""}
            ,{value:[30,7.77,-20],color:"#6A5ACD",x:"",y:"",desc:""}
            ,{value:[40,10.38,-20],color:"#6A5ACD",x:"",y:"",desc:"1038"}
            ,{value:[50,8.12,-20],color:"#6A5ACD",x:""}
            ,{value:[0,3.11,-30],color:"#6495ED",x:"",y:"OA",desc:""}
            ,{value:[10,3.84,-30],color:"#6495ED",x:"",y:"",desc:""}
            ,{value:[20,3.16,-30],color:"#6495ED",x:"",y:"",desc:""}
            ,{value:[30,2.34,-30],color:"#6495ED",x:"",y:"",desc:""}
            ,{value:[40,3.36,-30],color:"#6495ED",x:"",y:"",desc:""}
            ,{value:[50,2.38,-30],color:"#6495ED",x:""}
            ];
var position = [50,0,40];

function addLine(scene,data,position){
	var dataLine = {};
	var map_mater = {};
	for(var i=0;i<data.length;i++){
		var d = data[i];
		var color = d.color;
		if(typeof dataLine["L-"+d.value[2]] == 'undefined'){
			dataLine["L-"+d.value[2]] = [];
			dataLine["L-"+d.value[2]].push(d);
		}else{
			dataLine["L-"+d.value[2]].push(d);
		}
		if(typeof map_mater[color] == 'undefined'){
			map_mater[color] = new THREE.MeshLambertMaterial( {
				color: color,
				side: 0
			});
		}
	}
	for(var p in dataLine){
		var data1 = dataLine[p];
		var geometry_line = new THREE.Geometry();//声明一个几何体
		for(var i=0,len=data1.length;i<len;i++){
			var d = data1[i];
			geometry_line.vertices.push(new THREE.Vector3(d.value[0], d.value[1], d.value[2] ));
		}
		var line = new THREE.Line(geometry_line, map_mater[data1[0][color]], THREE.LinePieces);
//		scene.add(line);
	}
	
	
	var geometry_line = new THREE.Geometry();   //声明一个几何体
    var material_line = new THREE.LineBasicMaterial({ color: "#44ff00" ,linewidth: 10});   //定义材质外观
    var p1_line = new THREE.Vector3(0, 0, 0 );
    var p2_line = new THREE.Vector3(0, 0, 40 );
    geometry_line.vertices.push(p1_line); //vertices属性里可以存放点对象
    geometry_line.vertices.push(p2_line);
    var line = new THREE.Line(geometry_line, material_line, THREE.LinePieces);    //定义一条线
    scene.add(line);   //加入场景里
}

function loadFont(f){
	var loader = new THREE.FontLoader();
//	 loader.load("fonts/FZLanTingHeiS-UL-GB_Regular.json", function (res) {
		 loader.load("fonts/aa.json", function (res) {
		 window.res = res;
		 f(res);
//		 var font = new THREE.TextBufferGeometry("equipment走", {
//			  font: res,
//			  size: 5,
//			  height: 5
//		  }); 
//		  var material = new THREE.MeshLambertMaterial({side:THREE.DoubleSide,color: "#00ff00"}); 
//		  fontModel = new THREE.Mesh(font,material); 
//		  fontModel.position.set(0,0,0 );
//		  scene.add(fontModel); 
	 });
}
//raycaster = new THREE.Raycaster();
//mouse = new THREE.Vector2();
////对象拾取
//function pickupObjects(dom,e){
//    //将鼠标点击位置的屏幕坐标转成threejs中的标准坐标
//     var x = (e.clientX/dom.width())*2 -1;
//     var y = -(e.clientY/dom.height())*2 + 1;
//     //从相机发射一条射线，经过鼠标点击位置
//     raycaster.setFromCamera(mouse,camera);
//     //计算射线相机到的对象，可能有多个对象，因此返回的是一个数组，按离相机远近排列
//     let intersects = raycaster.intersectObjects(scene.children);
//     console.log("intersects",intersects);
//     for ( var i = 0; i < intersects.length; i++ ) {
//         //intersects[ i ].object.material.color.set( 0x00ff00 );
//     }
//}
//
//$("#container").click(function(e){
//	pickupObjects($("#container"),e);
//});